<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="js/jquery-1.12.4.js"></script>

</head>

<body>
    <script>

        function get(url, data, success) {
            const xhr = new XMLHttpRequest();

            if (Object.prototype.toString.call(url).slice(8, -1) === 'Object') {
                const option = url;
                url = option.url;
                data = option.data;
                success = option.success;
            }

            if (!!data) {
                const type = Object.prototype.toString.call(data).slice(8, -1)
                switch (type) {
                    case 'String':
                        url += '?' + data;
                        break;
                    case 'Function':
                        success = data;
                        data = undefined;
                        break;
                    case 'Object':
                        url += '?' + JSON.stringify(data).replace(/:/g, "=").replace(/"/g, '').replace(/,/g, '&').match(/\{([^)]*)\}/)[1]
                        break;
                }

            }

            xhr.open('get', url);
            xhr.send();
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4 && xhr.status == 200) {
                    success.call(xhr);
                }
            }
        }

        get('https://autumnfish.cn/api/joke/', function () {
            console.log(this.response);
            console.log('\n\n');
        })

        get({
            url: 'https://autumnfish.cn/api/joke/',
            success: function () {
                console.log(this.response);
                console.log('\n\n');
            }
        })


        get('https://autumnfish.cn/api/joke/list', 'num=5', function () {
            console.log(this.response);
            console.log('\n\n');
        })
        get({
            url: 'https://autumnfish.cn/api/joke/list',
            data: 'num=5',
            success: function () {
                console.log(this.response);
                console.log('\n\n');
            }
        })
        get('https://autumnfish.cn/api/joke/list', { num: 5 }, function () {
            console.log(this.response);
            console.log('\n\n');
        })
        get({
            url: 'https://autumnfish.cn/api/joke/list',
            data: { num: 5 },
            success: function () {
                console.log(this.response);
            }
        })
        
    </script>
</body>

</html>